import logging

import pytest

from resource.pom.calculator import Calculator


class Test:
    app = Calculator

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：进入计算器应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.stop_permission()
        device.refresh_layout()

        for exp in ['1+2', '3-4', '5*6', '8/2']:
            logging.info('计算 {}'.format(exp))
            expect_value = int(eval(exp))
            for i in exp:
                device.click_element(device.get_element_by_key(i))
            device.click_element(device.get_element_by_key('='))
            device.refresh_layout()
            actual_value = int(float(device.get_element_by_key('expression').get('text')))
            logging.info('预期结果：正常运行各种计算，结果正确 | {}={}?'.format(expect_value, actual_value))
            assert actual_value == expect_value, '计算结果不正确'
            # 删除计算结果
            device.click_element(device.get_element_by_key('C'))
